package com.cms.web.servlet;

import com.cms.pojo.Admin;
import com.cms.pojo.Course;
import com.cms.pojo.Sc;
import com.cms.service.CourseService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/cancelCourseServlet")
public class CancelCourseServlet extends HttpServlet {
    private CourseService courseService = new CourseService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 课程id
        int id = Integer.parseInt(request.getParameter("id"));

        HttpSession session = request.getSession();

        Admin user = (Admin) session.getAttribute("user");

        Sc sc = new Sc();
        sc.setC_id(id);
        sc.setS_id(user.getId());

        // 调用CourseService，删除sc表内容，减少课程已选课人数
        courseService.cancelCourse(sc);
        courseService.decreaseSelected(id);

        request.getRequestDispatcher("/selectedCourseServlet").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
